package com.xiaoniu.wifihotspotdemo;import android.Manifest;import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageManager;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.net.Uri;import android.net.wifi.ScanResult;import android.net.wifi.WifiConfiguration;import android.net.wifi.WifiInfo;import android.net.wifi.WifiManager;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.provider.Settings;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.webkit.PermissionRequest;import android.widget.AdapterView;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import com.xiaoniu.wifihotspotdemo.adapter.WifiListAdapter;import com.xiaoniu.wifihotspotdemo.thread.ConnectThread;import com.xiaoniu.wifihotspotdemo.thread.ListenerThread;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.FileReader;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.Socket;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ListView listView; private Button btn_create_hostspot; private Button btn_close_hostspot;// private Button btn_send;// private Button btn_search; private TextView textview; private TextView text_state; private WifiManager wifiManager; private ConnectivityManager mConnectivityManager; private WifiListAdapter wifiListAdapter; private WifiConfiguration config; private int wcgID; private static final String TAG = "MainActivity"; /** * 热点名称 */ private static final String WIFI_HOTSPOT_SSID = "TEST"; /** * 端口号 */ private static final int PORT = 54321; private static final int WIFICIPHER_NOPASS = 1; private static final int WIFICIPHER_WEP = 2; private static final int WIFICIPHER_WPA = 3;// public static final int DEVICE_CONNECTING = 1;//有设备正在连接热点// public static final int DEVICE_CONNECTED = 2;//有设备连上热点// public static final int SEND_MSG_SUCCSEE = 3;//发送消息成功// public static final int SEND_MSG_ERROR = 4;//发送消息失败// public static final int GET_MSG = 6;//获取新消息 /** * 连接线程 */ private ConnectThread connectThread; /** * 监听线程 */ private ListenerThread listenerThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initVIew(); initWithGetPermission(this); initBroadcastReceiver(); getRootPermission(getPackageCodePath());// wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);\ mConnectivityManager = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);// listenerThread = new ListenerThread(PORT, handler);// listenerThread.start(); } private void initVIew() { listView = (ListView) findViewById(R.id.listView); btn_create_hostspot = (Button) findViewById(R.id.btn_create_hostspot); btn_close_hostspot = (Button) findViewById(R.id.btn_close_hostspot);// btn_send = (Button) findViewById(R.id.btn_send);// btn_search = (Button) findViewById(R.id.btn_search); textview = (TextView) findViewById(R.id.textview); text_state = (TextView) findViewById(R.id.text_state); btn_create_hostspot.setOnClickListener(this); btn_close_hostspot.setOnClickListener(this);// btn_send.setOnClickListener(this);// btn_search.setOnClickListener(this); wifiListAdapter = new WifiListAdapter(this, R.layout.wifi_list_item); listView.setAdapter(wifiListAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { wifiManager.disconnect(); final ScanResult scanResult = wifiListAdapter.getItem(position); String capabilities = scanResult.capabilities; int type = WIFICIPHER_WPA;// if (!TextUtils.isEmpty(capabilities)) {// if (capabilities.contains("WPA") || capabilities.contains("wpa")) {// type = WIFICIPHER_WPA;// } else if (capabilities.contains("WEP") || capabilities.contains("wep")) {// type = WIFICIPHER_WEP;// } else {// type = WIFICIPHER_NOPASS;// }// }// config = isExsits(scanResult.SSID);// if (config == null) {// if (type != WIFICIPHER_NOPASS) {//需要密码// final EditText editText = new EditText(MainActivity.this);// final int finalType = type;// new AlertDialog.Builder(MainActivity.this).setTitle("请输入Wifi密码").setIcon(// android.R.drawable.ic_dialog_info).setView(// editText).setPositiveButton("确定", new DialogInterface.OnClickListener() {// @Override// public void onClick(DialogInterface dialog, int which) {// Log.w("AAA", "editText.getText():" editText.getText());// config = createWifiInfo(scanResult.SSID, editText.getText().toString(), finalType);// connect(config);// }// })// .setNegativeButton("取消", null).show();// return;// } else {// config = createWifiInfo(scanResult.SSID, "", type);// connect(config);// }// } else {// connect(config);// } } }); } private void connect(WifiConfiguration config) { text_state.setText("连接中..."); wcgID = wifiManager.addNetwork(config); wifiManager.enableNetwork(wcgID, true); } private void initBroadcastReceiver() { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(receiver, intentFilter); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_create_hostspot: createWifiHotspot();// setApEnable(this,true); break; case R.id.btn_close_hostspot: closeWifiHotspot(); break;// case R.id.btn_send:// if (connectThread != null) {// connectThread.sendData("这是来自Wifi热点的消息");// }else{// Log.w("AAA","connectThread == null");// }// break;// case R.id.btn_search:// search();// break; } } private static final int REQUEST_CODE_WRITE_SETTINGS = 2; public void initWithGetPermission(Activity context) { boolean permission; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { permission = Settings.System.canWrite(context); } else { permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED; } if (permission) { return; } else { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); intent.setData(Uri.parse("package:" context.getPackageName())); context.startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS); } else { ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_SETTINGS}, REQUEST_CODE_WRITE_SETTINGS); } } } public void setApEnable(Context context, boolean enable) { try { WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager == null) { return; } if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { // 如果是Android 8.0系统 if (enable) { // 打开热点 wifiManager.startLocalOnlyHotspot(new WifiManager.LocalOnlyHotspotCallback() { @Override public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) { super.onStarted(reservation); Log.d(TAG, "onStarted: "); } @Override public void onStopped() { super.onStopped(); Log.d(TAG, "onStopped: "); } @Override public void onFailed(int reason) { super.onFailed(reason); Log.d(TAG, "onFailed: "); } }, new Handler()); } else { // 关闭热点 Method method = wifiManager.getClass().getDeclaredMethod("stopSoftAp"); method.invoke(wifiManager); } } else { Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class); method.invoke(wifiManager, null, enable); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,非法参数异常"); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,非法访问异常"); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,调用目标异常"); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,安全异常"); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,空指针异常"); } catch (Exception e) { e.printStackTrace(); textview.setText("创建热点失败,报错"); } } /** * 创建Wifi热点 */ private void createWifiHotspot() { wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE); if (wifiManager.isWifiEnabled()) { //如果wifi处于打开状态,则关闭wifi, wifiManager.setWifiEnabled(false); } WifiConfiguration config = new WifiConfiguration(); config.SSID = WIFI_HOTSPOT_SSID; config.preSharedKey = "123456789"; config.hiddenSSID = true; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);//开放系统认证 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);// config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); int indexOfWPA2_PSK = 0;//从WifiConfiguration.KeyMgmt数组中查找WPA2_PSK的值 for (int i = 0; i < WifiConfiguration.KeyMgmt.strings.length; i ) { if (WifiConfiguration.KeyMgmt.strings[i].equals("WPA2_PSK")) { indexOfWPA2_PSK = i; break; } } config.allowedKeyManagement.set(indexOfWPA2_PSK); Log.d(TAG, "createWifiHotspot: -----=" indexOfWPA2_PSK); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; //通过反射调用设置热点 try {// Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);// boolean enable = (Boolean) method.invoke(wifiManager, config, true); Method method = wifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class,Boolean.TYPE); boolean enable = (Boolean)method.invoke(wifiManager,config,true); Log.d(TAG, "createWifiHotspot: 00--00:" method); Log.d(TAG, "createWifiHotspot: 11--11:" enable); if (enable) { textview.setText("热点已开启 SSID:" config.SSID " password:" config.preSharedKey); } else { textview.setText("创建热点失败,false"); } } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,非法参数异常"); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,非法访问异常"); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,调用目标异常"); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,安全异常"); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); textview.setText("创建热点失败,空指针异常"); } catch (Exception e) { e.printStackTrace(); textview.setText("创建热点失败,报错"); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 判断是否有WRITE_SETTINGS权限 if (Settings.System.canWrite(this)) { //创建热点 Log.d(TAG, "onActivityResult: 111111"); } else { //没有权限 Log.d(TAG, "onActivityResult: 222222"); } } } super.onActivityResult(requestCode, resultCode, data); } /** * 关闭WiFi热点 */ public void closeWifiHotspot() { try { Method method = wifiManager.getClass().getMethod("getWifiApConfiguration"); method.setAccessible(true); WifiConfiguration config = (WifiConfiguration) method.invoke(wifiManager); Method method2 = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class,boolean.class); method2.invoke(wifiManager, config,false); Log.d(TAG, "closeWifiHotspot: aa--aa:" method2); Log.d(TAG, "closeWifiHotspot: bb--bb:" method2.invoke(wifiManager, config,false)); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } textview.setText("热点已关闭"); } /** * 获取连接到热点上的手机ip * * @return */ private ArrayList<String> getConnectedIP() { ArrayList<String> connectedIP = new ArrayList<String>(); try { BufferedReader br = new BufferedReader(new FileReader( "/proc/net/arp")); String line; while ((line = br.readLine()) != null) { String[] splitted = line.split(" "); if (splitted != null && splitted.length >= 4) { String ip = splitted[0]; connectedIP.add(ip); } } } catch (Exception e) { e.printStackTrace(); } return connectedIP; } /** * 搜索wifi热点 */ private void search() { if (!wifiManager.isWifiEnabled()) { //开启wifi wifiManager.setWifiEnabled(true); } wifiManager.startScan(); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(receiver); }// private Handler handler = new Handler() {// @Override// public void handleMessage(Message msg) {// switch (msg.what) {// case DEVICE_CONNECTING:// connectThread = new ConnectThread(listenerThread.getSocket(),handler);// connectThread.start();// break;// case DEVICE_CONNECTED:// textview.setText("设备连接成功");// break;// case SEND_MSG_SUCCSEE:// textview.setText("发送消息成功:" msg.getData().getString("MSG"));// break;// case SEND_MSG_ERROR:// textview.setText("发送消息失败:" msg.getData().getString("MSG"));// break;// case GET_MSG:// textview.setText("收到消息:" msg.getData().getString("MSG"));// break;// }// }// };// public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";// public static final String EXTRA_WIFI_AP_STATE = "wifi_state"; private BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { Log.w("BBB", "SCAN_RESULTS_AVAILABLE_ACTION"); // wifi已成功扫描到可用wifi。 List<ScanResult> scanResults = wifiManager.getScanResults(); wifiListAdapter.clear(); wifiListAdapter.addAll(scanResults); } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { Log.w("BBB", "WifiManager.WIFI_STATE_CHANGED_ACTION"); int wifiState = intent.getIntExtra( WifiManager.EXTRA_WIFI_STATE, 0); switch (wifiState) { case WifiManager.WIFI_STATE_ENABLED: //获取到wifi开启的广播时,开始扫描 wifiManager.startScan(); break; case WifiManager.WIFI_STATE_DISABLED: //wifi关闭发出的广播 break; } } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { Log.w("BBB", "WifiManager.NETWORK_STATE_CHANGED_ACTION"); NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) { text_state.setText("连接已断开"); } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final WifiInfo wifiInfo = wifiManager.getConnectionInfo(); text_state.setText("已连接到网络:" wifiInfo.getSSID()); Log.w("AAA", "wifiInfo.getSSID():" wifiInfo.getSSID() " WIFI_HOTSPOT_SSID:" WIFI_HOTSPOT_SSID); if (wifiInfo.getSSID().equals(WIFI_HOTSPOT_SSID)) { //如果当前连接到的wifi是热点,则开启连接线程 new Thread(new Runnable() { @Override public void run() { try { ArrayList<String> connectedIP = getConnectedIP(); for (String ip : connectedIP) { if (ip.contains(".")) { Log.w("AAA", "IP:" ip); Socket socket = new Socket(ip, PORT);// connectThread = new ConnectThread(socket, handler); connectThread.start(); } } } catch (IOException e) { e.printStackTrace(); } } }).start(); } }else { NetworkInfo.DetailedState state = info.getDetailedState(); if (state == state.CONNECTING) { text_state.setText("连接中..."); } else if (state == state.AUTHENTICATING) { text_state.setText("正在验证身份信息..."); } else if (state == state.OBTAINING_IPADDR) { text_state.setText("正在获取IP地址..."); } else if (state == state.FAILED) { text_state.setText("连接失败"); } } } /* else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { text_state.setText("连接已断开"); wifiManager.removeNetwork(wcgID); } else { WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); text_state.setText("已连接到网络:" wifiInfo.getSSID()); } }*/ } }; /** * 判断当前wifi是否有保存 * * @param SSID * @return */ private WifiConfiguration isExsits(String SSID) { List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" SSID "\"")) { return existingConfig; } } return null; } public WifiConfiguration createWifiInfo(String SSID, String password, int type) { Log.w("AAA", "SSID = " SSID "password " password "type =" type); WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" SSID "\""; if (type == WIFICIPHER_NOPASS) { config.wepKeys[0] = "\"" "\""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } else if (type == WIFICIPHER_WEP) { config.preSharedKey = "\"" password "\""; config.hiddenSSID = true; config.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.CCMP); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.WEP40); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.WEP104); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0; } else if (type == WIFICIPHER_WPA) { config.preSharedKey = "\"" password "\""; config.hiddenSSID = true; config.allowedAuthAlgorithms .set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.TKIP); config.allowedKeyManagement .set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.TKIP); // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.allowedGroupCiphers .set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers .set(WifiConfiguration.PairwiseCipher.CCMP); config.status = WifiConfiguration.Status.ENABLED; } else { return null; } return config; } public static boolean getRootPermission(String pkgCodePath) { Process process = null; DataOutputStream os = null; try { String cmd1 = "chmod 777 " pkgCodePath; String cmd2 = "setenforce 0"; process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd1 "\n"); os.writeBytes(cmd2 "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } return true; }}
评论